
package com.eltonkent.db;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.eltonkent.security.BFEncryptionEngine;

public final class CredentialTable extends GenericTable
{
	final static String TABLE_NAME = "TBL_CREDENTIALS";

	private final String COL_PASSWORD = "PASSWORD";

	private final String COL_ADDED_DATE = "DATE_CREATED";
	private final String COL_LAST_LOGIN = "LAST_LOGIN";
	public static String lastLogin;

	CredentialTable()
	{
		super();
	}

	public CredentialTable(Context context)
	{
		super(context);
	}

	@Override
	void doAfterTableCreate(SQLiteDatabase database)
	{
	}

	public String getLastLoginDate()
	{
		return lastLogin;
	}

	public String getPassword()
	{
		Cursor query = getDatabase().query(TABLE_NAME, new String[]
		{ COL_PASSWORD, COL_LAST_LOGIN }, null, null, null, null, null);
		byte[] passwordEnc = null;
		String password = null;
		if(query.getCount() > 0)
		{
			query.moveToFirst();
			passwordEnc = query.getBlob(query.getColumnIndex(COL_PASSWORD));
			lastLogin = query.getString(query.getColumnIndex(COL_LAST_LOGIN));
			query.close();

			password = getPassword(passwordEnc);

			// if(passwordEnc != null)
			// {
			// passwordEnc =
			// BFEncryptionEngine.getInstance().decrypt(passwordEnc);
			// try
			// {
			// password = new String(passwordEnc, "UTF-8");
			// } catch(UnsupportedEncodingException e)
			// {
			// e.printStackTrace();
			// }
			// }
		}

		closeDB();
		return password;
	}

	public String getPassword(byte[] enc)
	{
		byte[] dec = BFEncryptionEngine.getInstance().decrypt(enc);
		try
		{
			return new String(dec, "UTF-8");
		} catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		closeDB();
		return null;
	}

	@Override
	List<Field> getTableFields()
	{
		List<Field> fields = new ArrayList<Field>();
		fields.add(new Field(COL_PASSWORD, "blob"));
		fields.add(new Field(COL_ADDED_DATE, "text"));
		fields.add(new Field(COL_LAST_LOGIN, "text"));
		return fields;
	}

	@Override
	String getTableName()
	{
		return TABLE_NAME;
	}

	public void savePassword(String password, String date)
	{
		ContentValues values = new ContentValues();
		byte[] passwordPlain = null;
		try
		{
			passwordPlain = password.getBytes("UTF-8");
		} catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		passwordPlain = BFEncryptionEngine.getInstance().encrypt(passwordPlain);
		Log.d("UI", "Password after  encryption" + passwordPlain);
		values.put(COL_PASSWORD, passwordPlain);
		values.put(COL_ADDED_DATE, date);
		getDatabase().insert(TABLE_NAME, null, values);
		closeDB();
	}

	public void updateLastLogin(String setupDate, String lastLoginDate)
	{
		ContentValues values = new ContentValues();
		values.put(COL_LAST_LOGIN, lastLoginDate);
		getDatabase().update(TABLE_NAME, values, COL_ADDED_DATE + '=' + "'" + setupDate + "'", null);
		closeDB();
	}

	@Override
	public void deSerialize(DataInputStream dis) throws IOException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void serialize(DataOutputStream dos) throws IOException
	{
		Cursor query = getDatabase().query(TABLE_NAME, new String[]
		{ COL_PASSWORD }, null, null, null, null, null);
		query.moveToFirst();
		byte[] password = query.getBlob(query.getColumnIndex(COL_PASSWORD));
		dos.writeInt(password.length);
		dos.write(password);
		query.close();
		closeDB();

	}
}
